﻿using SL651Center2.x.Enums;
using SL651Center2.x.Messages;
using System.Globalization;
using TouchSocket.Core;

namespace SL651Center2.x.ReadMessage
{
    public class ReadASCIIData
    {
        public static ReadASCIIData Instance { get; } = new ReadASCIIData();

        private ReadASCIIData()
        {
            _bytes = new List<byte>();
        }

        readonly List<byte> _bytes;

        public IMessage Message { get; private set; }

        public void ReadASCIIMessage(in ByteBlock byteBlock, FunctionCode functionCode)
        {
            switch (functionCode)
            {
                case FunctionCode.FC_30:
                case FunctionCode.FC_32:
                case FunctionCode.FC_33:
                case FunctionCode.FC_34://小时报
                case FunctionCode.FC_37:
                case FunctionCode.FC_3A:
                case FunctionCode.FC_44:
                    //要素数据
                    {
                        MessageElement message = new MessageElement();
                        message.ReadASCII(byteBlock);
                        Message = message;
                        break;
                    }
                case FunctionCode.FC_31://有时间步长码
                case FunctionCode.FC_38://有时间步长码
                    //均匀时段报
                    {
                        MessageTime messageTime = new MessageTime();
                        messageTime.ReadASCII(byteBlock);
                        Message = messageTime;
                        break;
                    }
                case FunctionCode.FC_35:
                case FunctionCode.FC_39:
                    //人工置数报
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        if (Enum.IsDefined(typeof(ElementCode), arr[0]))
                        {
                            var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[0]);

                            var (name, unit) = ElementInfo.Instance.GetInfo(elementCode);
                            Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name}");

                            for (int i = 1; i < arr.Length - 1; i += 2)
                            {
                                var (n, un) = ManualEntryInfo.GetInfo(arr[i]);
                                Console.WriteLine($"名称：{n},数据：{arr[i + 1]}{un}");
                            }

                        }
                        break;
                    }
                case FunctionCode.FC_36:
                    //图片
                    {
                        MessageImg messageImg = new MessageImg();
                        messageImg.ReadASCII(byteBlock);
                        Message = messageImg;
                        /*var serialnum = byteBlock.ReadToArray(4);
                        var snum = serialnum.ToASCIIString();
                        Convert.ToInt32(snum, 16);
                        Console.WriteLine($"流水号:{Convert.ToUInt16(snum, 16)}--{snum}");

                        var timeb = byteBlock.ReadToArray(12);
                        var timeHEX = timeb.ToASCIIString();
                        var timeString = $"{timeHEX[..2]}年{timeHEX.Substring(2, 2)}月{timeHEX.Substring(4, 2)}日{timeHEX.Substring(6, 2)}:{timeHEX.Substring(8, 2)}:{timeHEX.Substring(10)}";
                        Console.WriteLine($"发报时间:{timeString}");

                        var data = byteBlock.ReadToArray(30);
                        var datastr = data.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        var typestr = arr[2];
                        if (Enum.IsDefined(typeof(TelemetryStationCode), typestr))
                        {
                            var typecode = (TelemetryStationCode)Enum.Parse(typeof(TelemetryStationCode), typestr);
                            var typename = TelemetryStationInfo.GetInfo(typecode);
                            Console.WriteLine($"遥测站分类码:{Helper.ToHEXString((byte)typecode)}-{typestr}-{typename}");
                        }

                        var tt = arr[4];
                        var tString = $"{tt[0..2]}年{tt[2..4]}月{tt[4..6]}日{tt[6..8]}:{tt[8..10]}";
                        Console.WriteLine($"观测时间:{tString}");

                        var elem = byteBlock.ReadToArray(3);
                        var elemstr = elem.ToASCIIString();
                        if (Enum.IsDefined(typeof(ElementCode), elemstr))
                        {
                            var elemCode = (ElementCode)Enum.Parse(typeof(ElementCode), elemstr);

                            //跳过数据定义
                            byteBlock.Seek(1, SeekOrigin.Current);

                            var (name, unit) = ElementInfo.Instance.GetInfo(elemCode);

                            Console.WriteLine(name);

                            //图片信息
                            if (elemCode == ElementCode.PIC)
                            {
                                var img = byteBlock.ToArray(byteBlock.Pos);
                                Console.WriteLine(Helper.ByteToImg(img.ToArray(),"ASCII.jpg"));
                            }

                        }*/
                    }
                    break;
                case FunctionCode.FC_40:
                case FunctionCode.FC_41:
                    //基本配置表
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        for (int i = 2; i < arr.Length - 1; i++)
                        {
                            var code = arr[i];
                            switch (code)
                            {
                                //中心站地址
                                case "01":
                                    {
                                        var centers = arr[++i];
                                        Console.WriteLine($"中心站1地址：{centers[..2]}");
                                        Console.WriteLine($"中心站2地址：{centers[2..4]}");
                                        Console.WriteLine($"中心站3地址：{centers[4..6]}");
                                        Console.WriteLine($"中心站4地址：{centers[6..]}");
                                    }
                                    break;
                                //遥测站地址
                                case "02":
                                    {
                                        Console.WriteLine($"遥测站地址:{arr[++i]}");
                                        break;
                                    }
                                //密码
                                case "03":
                                    {
                                        Console.WriteLine($"密码:{arr[++i]}");
                                        break;
                                    }
                                //中心站1主信道类型及地址
                                case "04":
                                    {
                                        Console.WriteLine("中心站1主信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //中心站1备用信道类型及地址
                                case "05":
                                    {
                                        Console.WriteLine("中心站1备用信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //中心站2主信道类型及地址
                                case "06":
                                    {
                                        Console.WriteLine("中心站2主信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //中心站2备用信道类型及地址
                                case "07":
                                    {
                                        Console.WriteLine("中心站2备用信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //中心站3主信道类型及地址
                                case "08":
                                    {
                                        Console.WriteLine("中心站3主信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //中心站3备用信道类型及地址
                                case "09":
                                    {
                                        Console.WriteLine("中心站3备用信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //中心站4主信道类型及地址
                                case "0A":
                                    {
                                        Console.WriteLine("中心站4主信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //中心站4备用信道类型及地址
                                case "0B":
                                    {
                                        Console.WriteLine("中心站4备用信道类型及地址：");
                                        var d = arr[++i];
                                        var xindao = d[..2];
                                        var xin = Convert.ToInt16(xindao, 16);
                                        var addr = d[2..];
                                        if (xin == 0)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-禁用");
                                        }
                                        else if (xin == 1)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                        }
                                        else if (xin == 2)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                        }
                                        else if (xin == 3)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                        }
                                        else if (xin == 4)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                        }
                                        else if (xin == 5)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                        }
                                        else if (xin == 6)
                                        {
                                            Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                        }
                                        break;
                                    }
                                //工作方式
                                case "0C":
                                    {
                                        var workType = arr[++i];
                                        var wt = Convert.ToInt16(workType, 16);
                                        if (wt == 1)
                                        {
                                            Console.WriteLine($"工作方式:{wt}-自报工作状态");
                                        }
                                        else if (wt == 2)
                                        {
                                            Console.WriteLine($"工作方式:{wt}-自报确认工作状态");
                                        }
                                        else if (wt == 3)
                                        {
                                            Console.WriteLine($"工作方式:{wt}-查询/应答工作状态");
                                        }
                                        else if (wt == 4)
                                        {
                                            Console.WriteLine($"工作方式:{wt}-调试或维修状态");
                                        }
                                        break;
                                    }
                                //遥测站采集要素设置
                                case "0D":
                                    {
                                        var d = arr[++i];
                                        var a1 = d[..2];
                                        var a1byte = byte.Parse(a1, NumberStyles.HexNumber);
                                        var a1bin = Helper.ByteToBinString(a1byte);
                                        var a1str = $"降水量：{a1bin[0]}，蒸发量：{a1bin[1]}，风向：{a1bin[2]}，风速：{a1bin[3]}，气温：{a1bin[4]}，湿度：{a1bin[5]}，地温：{a1bin[6]}，气压：{a1bin[7]}";
                                        Console.WriteLine(a1str);

                                        var a2 = d[2..4];
                                        var a2byte = byte.Parse(a2, NumberStyles.HexNumber);
                                        var a2bin = Helper.ByteToBinString(a2byte);
                                        var a2str = $"水位8：{a2bin[0]}，水位7：{a2bin[1]}，水位6：{a2bin[2]}，水位5：{a2bin[3]}，水位4：{a2bin[4]}，水位3：{a2bin[5]}，水位2：{a2bin[6]}，水位1：{a2bin[7]}";
                                        Console.WriteLine(a2str);

                                        var a3 = d[4..6];
                                        var a3byte = byte.Parse(a3, NumberStyles.HexNumber);
                                        var a3bin = Helper.ByteToBinString(a3byte);
                                        var a3str = $"地下水埋深：{a3bin[0]}，图片：{a3bin[1]}，波浪：{a3bin[2]}，闸门开度：{a3bin[3]}，水量：{a3bin[4]}，流速：{a3bin[5]}，流量：{a3bin[6]}，水压：{a3bin[7]}";
                                        Console.WriteLine(a3str);

                                        var a4 = d[6..8];
                                        var a4byte = byte.Parse(a4, NumberStyles.HexNumber);
                                        var a4bin = Helper.ByteToBinString(a4byte);
                                        var a4str = $"水表8：{a4bin[0]}，水表7：{a4bin[1]}，水表6：{a4bin[2]}，水表5：{a4bin[3]}，水表4：{a4bin[4]}，水表3：{a4bin[5]}，水表2：{a4bin[6]}，水表1：{a4bin[7]}";
                                        Console.WriteLine(a4str);

                                        var a5 = d[8..10];
                                        var a5byte = byte.Parse(a5, NumberStyles.HexNumber);
                                        var a5bin = Helper.ByteToBinString(a5byte);
                                        var a5str = $"100厘米墒情：{a5bin[0]}，80厘米墒情：{a5bin[1]}，60厘米墒情：{a5bin[2]}，50厘米墒情：{a5bin[3]}，40厘米墒情：{a5bin[4]}，30厘米墒情：{a5bin[5]}，20厘米墒情：{a5bin[6]}，10厘米墒情：{a5bin[7]}";
                                        Console.WriteLine(a5str);

                                        var a6 = d[10..12];
                                        var a6byte = byte.Parse(a6, NumberStyles.HexNumber);
                                        var a6bin = Helper.ByteToBinString(a6byte);
                                        var a6str = $"pH值：{a6bin[0]}，溶解氧：{a6bin[1]}，电导率：{a6bin[2]}，浊度：{a6bin[3]}，氧化还原电位：{a6bin[4]}，高猛酸盐指数：{a6bin[5]}，氨氮：{a6bin[6]}，水温：{a5bin[7]}";
                                        Console.WriteLine(a6str);

                                        var a7 = d[12..14];
                                        var a7byte = byte.Parse(a7, NumberStyles.HexNumber);
                                        var a7bin = Helper.ByteToBinString(a7byte);
                                        var a7str = $"总有机碳：{a7bin[0]}，总氮：{a7bin[1]}，总磷：{a7bin[2]}，锌：{a7bin[3]}，硒：{a7bin[4]}，砷：{a7bin[5]}，总汞：{a7bin[6]}，镉：{a7bin[7]}";
                                        Console.WriteLine(a7str);

                                        var a8 = d[14..];
                                        var a8byte = byte.Parse(a8, NumberStyles.HexNumber);
                                        var a8bin = Helper.ByteToBinString(a8byte);
                                        var a8str = $"{a8bin[0]}，{a8bin[1]}，{a8bin[2]}，{a8bin[3]}，{a8bin[4]}，叶绿素a：{a8bin[5]}，铜：{a8bin[6]}，铅：{a8bin[7]}";
                                        Console.WriteLine(a8str);

                                        break;
                                    }
                                //中继站(集合转发站)服务地址范围
                                case "0E":
                                    {
                                        var d = arr[++i];
                                        Console.WriteLine($"中继站(集合转发站)服务地址范围:{d}");
                                        break;
                                    }
                                //遥测站通信设备识别号
                                case "0F":
                                    {
                                        var d = arr[++i];

                                        var sim = d[..2];
                                        var st = Convert.ToInt16(sim, 16);
                                        if (st == 1)
                                        {
                                            Console.WriteLine($"卡类型：{st}-移动通信卡,卡识别号：{d[2..]}");
                                        }
                                        else if (st == 2)
                                        {
                                            Console.WriteLine($"卡类型：{st}-北斗卫星通信卡,卡识别号：{d[2..]}");
                                        }
                                        else
                                        {
                                            Console.WriteLine($"遥测站通信设备识别号:{d}");
                                        }
                                        break;
                                    }
                                default:
                                    break;
                            }
                        }
                    }
                    break;
                case FunctionCode.FC_42:
                case FunctionCode.FC_43:
                    //运行参数表
                    {
                        MessageRunConfig message = new MessageRunConfig();
                        message.ReadASCII(byteBlock);
                        Message = message;
                    }
                    break;
                case FunctionCode.FC_45:
                    //软件版本
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        Console.WriteLine(arr[2]);

                        break;
                    }
                case FunctionCode.FC_46:
                case FunctionCode.FC_4B:
                    //遥测站状态和报警信息
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        if (arr[2] == "ZT")
                        {
                            var d = arr[4];
                            var a1byte = byte.Parse(d, NumberStyles.HexNumber);
                            var binStr = Helper.ByteToBinString(a1byte);
                            var str = binStr.ToCharArray();
                            Array.Reverse(str);

                            Console.WriteLine(binStr);

                            if (str[0] == '0')
                            {
                                Console.WriteLine($"交流电充电状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("交流电充电状态：停电");
                            }
                            if (str[1] == '0')
                            {
                                Console.WriteLine("蓄电池电压状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("蓄电池电压状态：电压低");
                            }
                            if (str[2] == '0')
                            {
                                Console.WriteLine("水位超限报警状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("水位超限报警状态：报警");
                            }
                            if (str[3] == '0')
                            {
                                Console.WriteLine("流量超限报警状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("流量超限报警状态：报警");
                            }
                            if (str[4] == '0')
                            {
                                Console.WriteLine("水质超限报警状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("水质超限报警状态：报警");
                            }
                            if (str[5] == '0')
                            {
                                Console.WriteLine("流量仪表状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("流量仪表状态：故障");
                            }
                            if (str[6] == '0')
                            {
                                Console.WriteLine("水位仪表状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("水位仪表状态：故障");
                            }
                            if (str[7] == '0')
                            {
                                Console.WriteLine("终端箱门状态：开启");
                            }
                            else
                            {
                                Console.WriteLine("终端箱门状态：关闭");
                            }
                            if (str[8] == '0')
                            {
                                Console.WriteLine("存储器状态：正常");
                            }
                            else
                            {
                                Console.WriteLine("存储器状态：异常");
                            }
                            if (str[9] == '0')
                            {
                                Console.WriteLine("IC卡功能有效：关闭");
                            }
                            else
                            {
                                Console.WriteLine("IC卡功能有效：IC卡有效");
                            }
                            if (str[10] == '0')
                            {
                                Console.WriteLine("水泵工作状态：水泵工作");
                            }
                            else
                            {
                                Console.WriteLine("水泵工作状态：水泵停机");
                            }
                            if (str[11] == '0')
                            {
                                Console.WriteLine("剩余水量报警：未超限");
                            }
                            else
                            {
                                Console.WriteLine("剩余水量报警：水量超限");
                            }

                        }
                        break;
                    }
                case FunctionCode.FC_47://初始化固态存储数据
                case FunctionCode.FC_48://恢复出厂设置
                case FunctionCode.FC_4A://设置遥测站时钟
                case FunctionCode.FC_51://查询遥测站时钟
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");
                        break;
                    }
                case FunctionCode.FC_49:
                    //修改密码
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        var pwd = arr[3];
                        Console.WriteLine($"新密码:{pwd}");

                        break;
                    }
                case FunctionCode.FC_4C:
                    //水泵状态
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        var l = arr[2];
                        var bl = l[2..];
                        var blb = byte.Parse(bl, NumberStyles.HexNumber);
                        var bs = Helper.ByteToBinString(blb);
                        var str = bs.ToCharArray();
                        Array.Reverse(str);
                        for (int i = 0; i < str.Length; i++)
                        {
                            string t = str[i] == '0' ? "关闭" : "开启";
                            Console.Write($"{i + 1}号水泵状态：{str[i]}-{t}");
                        }
                        break;
                    }
                case FunctionCode.FC_4D:
                    //阀门状态
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        var l1 = arr[2];
                        var bl1 = l1[2..];
                        var blb1 = byte.Parse(bl1, NumberStyles.HexNumber);
                        var bs1 = Helper.ByteToBinString(blb1);
                        var str = bs1.ToCharArray();
                        Array.Reverse(str);
                        for (int i = 0; i < str.Length; i++)
                        {
                            string t = str[i] == '0' ? "关闭" : "开启";
                            Console.Write($"{i + 1}号阀门状态：{str[i]}-{t}");
                        }
                        break;
                    }
                case FunctionCode.FC_4E:
                    //闸门状态
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        var jl = arr[2];
                        var jm = jl[2..];
                        var jmb = byte.Parse(jm, NumberStyles.HexNumber);
                        var jms = Helper.ByteToBinString(jmb);
                        var str = jms.ToCharArray();
                        Array.Reverse(str);
                        for (int i = 0; i < str.Length; i++)
                        {
                            string t = str[i] == '0' ? "关闭" : "开启";
                            Console.Write($"{i + 1}号闸门状态：{str[i]}-{t}");
                        }

                        for (int i = 3; i < arr.Length - 1; i++)
                        {
                            var kd = arr[i];
                            var kdb = byte.Parse(kd);
                            var v = Helper.ByteToBCDInt(kdb);
                            Console.WriteLine($"{i + 1}号闸门开度：{v}cm");
                        }

                        break;
                    }
                case FunctionCode.FC_4F:
                    //水量定值
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        var cz = arr[2];
                        if (cz == "FF")
                        {
                            Console.WriteLine("定值控制投入");
                        }
                        else if (cz == "00")
                        {
                            Console.WriteLine("定值控制退出");
                        }
                        break;
                    }
                case FunctionCode.FC_50:
                    //遥测站事件记录
                    {
                        var datab = byteBlock.ToArray(byteBlock.Position);
                        var datastr = datab.ToASCIIString();
                        Console.WriteLine(datastr);

                        var arr = datastr.Split(" ");
                        Console.WriteLine($"遥测站地址:{arr[1]}");

                        ReadOnlySpan<char> cs = arr[2].AsSpan();
                        for (int i = 1, j = 0; i < 33; i++, j += 4)
                        {
                            var num = Convert.ToUInt16(cs.Slice(j, 4).ToString(), 16);
                            switch (i)
                            {
                                case 1:
                                    Console.WriteLine($"ERC{i}-历史数据初始化记录：{num}");
                                    break;
                                case 2:
                                    Console.WriteLine($"ERC{i}-参数变更记录：{num}");
                                    break;
                                case 3:
                                    Console.WriteLine($"ERC{i}-状态量变位记录：{num}");
                                    break;
                                case 4:
                                    Console.WriteLine($"ERC{i}-传感器及仪表故障记录：{num}");
                                    break;
                                case 5:
                                    Console.WriteLine($"ERC{i}-密码修改记录：{num}");
                                    break;
                                case 6:
                                    Console.WriteLine($"ERC{i}-终端故障记录：{num}");
                                    break;
                                case 7:
                                    Console.WriteLine($"ERC{i}-交流失电记录：{num}");
                                    break;
                                case 8:
                                    Console.WriteLine($"ERC{i}-蓄电池电压低告警记录：{num}");
                                    break;
                                case 9:
                                    Console.WriteLine($"ERC{i}-终端箱非法打开记录：{num}");
                                    break;
                                case 10:
                                    Console.WriteLine($"ERC{i}-水泵故障记录：{num}");
                                    break;
                                case 11:
                                    Console.WriteLine($"ERC{i}-剩余水量超限告警记录：{num}");
                                    break;
                                case 12:
                                    Console.WriteLine($"ERC{i}-水位超限告警记录：{num}");
                                    break;
                                case 13:
                                    Console.WriteLine($"ERC{i}-水压超限告警记录：{num}");
                                    break;
                                case 14:
                                    Console.WriteLine($"ERC{i}-水质参数超限告警记录：{num}");
                                    break;
                                case 15:
                                    Console.WriteLine($"ERC{i}-数据出错记录：{num}");
                                    break;
                                case 16:
                                    Console.WriteLine($"ERC{i}-发报文记录：{num}");
                                    break;
                                case 17:
                                    Console.WriteLine($"ERC{i}-收报文记录：{num}");
                                    break;
                                case 18:
                                    Console.WriteLine($"ERC{i}-发报文出错记录：{num}");
                                    break;
                                default:
                                    Console.WriteLine($"ERC{i}-：{num}");
                                    break;
                            }
                        }

                        break;
                    }
                default:
                    break;

            }
        }

        public void ReadASCIIMessage(in ByteBlock byteBlock, MessageFrame messageFrame)
        {
            //判断是否多包传输
            if (messageFrame.StartMark == StartMarks.SYN)
            {
                //读取正文
                var bytes = byteBlock.ToArray(byteBlock.Position, messageFrame.MessageLength);
                //保存正文
                _bytes.AddRange(bytes);

                //如果是最后一个包
                if (messageFrame.PackageIndex == messageFrame.PackageCount)
                {
                    //创建字节流
                    ByteBlock block = new ByteBlock(_bytes.Count);
                    //将正文写入字节流
                    block.Write(_bytes.ToArray());
                    //设置游标到起始位置
                    block.SeekToStart();
                    //解析正文
                    ReadASCIIMessage(block, messageFrame.FunctionCode);
                    //清除保存的正文
                    _bytes.Clear();
                    //释放字节流
                    block.Dispose();
                }
            }
            else
            {
                ReadASCIIMessage(byteBlock, messageFrame.FunctionCode);
            }

        }
    }
}
